package easy;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution_144 {

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
//        digui(root, list);

        Stack<TreeNode> stack = new Stack<>();
        while (!stack.empty() || root!=null) {
            if (root == null) {
                root = stack.pop();
                root = root.right;
            } else {
                list.add(root.val);
                stack.add(root);
                root = root.left;
            }
        }
        return list;
    }

    public void digui(TreeNode node, List<Integer> list) {
        if (node == null) {
            return;
        } else {
            list.add(node.val);
            digui(node.left, list);
            digui(node.right, list);
        }
    }

    public static void main(String[] args) {
        Solution_144 main = new Solution_144();
        TreeNode root = new TreeNode(1);
        root.right = new TreeNode(2);
        root.right.left = new TreeNode(3);
        List<Integer> list = main.inorderTraversal(root);
        for (Integer integer : list) {
            System.out.print(integer+",");
        }
        System.out.println();
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}